package com.iseastar.guojiang.newcabinet;

import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.iseastar.guojiang.BaseFragment;
import com.iseastar.guojiang.consts.MsgID;
import com.iseastar.guojiang.dialog.DialogMgr;
import com.iseastar.guojiang.logic.AppHttp;
import com.iseastar.guojiang.logic.JSON;
import com.iseastar.guojiang.model.ReqResult;
import com.iseastar.guojiang.model.ServiceTaskBean;
import com.iseastar.guojiang.model.TaskDetailBackBean;
import com.iseastar.guojiang.newcabinet.adapter.SameCityTaskDetailAdapter;
import com.shouhuobao.station.R;

import droid.frame.utils.lang.Str;
import droid.frame.utils.sqlite.DateUtils;

/**
 * Created by joker on 2017/12/28.
 * 同城任务详情
 */
public class SameCityTaskDetailFragment extends BaseFragment implements View.OnClickListener {

    private TextView mSendOrderTimeTV;//派单时间
    private TextView mTaskDetailDescTV;//任务描述
    private TextView mTakeAddressNameTV;//取货地址名字
    private TextView mTakeAddressDetailsTV;//取货的详细地址
    private TextView mSendAddressNameTV;//送货地址名字
    private TextView mSendAddressDetailsTV;//送货的详细地址
    private TextView mSendContractTV;//寄件人信息
    private TextView mReceiverContractTV;//收件人信息
    private TextView mTaskCodeTV;//任务编号
    private ListView mListView;
    private SameCityTaskDetailAdapter mAdapter = null;
    private ServiceTaskBean serviceTaskBean = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (null != bundle) {
            serviceTaskBean = (ServiceTaskBean) bundle.getSerializable("item");
            showLoadingDialog(null);
            AppHttp.getInstance().getCourierTaskDetailData(serviceTaskBean.getTaskId());
        }
    }

    @Override
    protected void findViewById() {
        setContentView(R.layout.fragment_samecity_task_detail);
        super.findViewById();
        mSendOrderTimeTV = (TextView) findViewById(R.id.send_order_time_tv);
        mTaskDetailDescTV = (TextView) findViewById(R.id.task_desc_tv);
        mTakeAddressNameTV = (TextView) findViewById(R.id.take_build_name_tv);
        mTakeAddressDetailsTV = (TextView) findViewById(R.id.take_address_details_tv);
        mSendAddressNameTV = (TextView) findViewById(R.id.send_build_name_tv);
        mSendAddressDetailsTV = (TextView) findViewById(R.id.send_address_details_tv);
        mSendContractTV = (TextView) findViewById(R.id.send_contract_tv);
        findViewById(R.id.send_phone_iv).setOnClickListener(this);
        mReceiverContractTV = (TextView) findViewById(R.id.receiver_contract_tv);
        findViewById(R.id.receiver_phone_iv).setOnClickListener(this);
        mTaskCodeTV = (TextView) findViewById(R.id.task_code_tv);
        mListView = (ListView) findViewById(R.id.listview);
        mAdapter = new SameCityTaskDetailAdapter(null, getActivityContext(), mListView);
        mListView.setAdapter(mAdapter);
        updateView();
    }

    /**
     * 更新布局数据展示
     */
    public void updateView() {
        if (serviceTaskBean != null) {
            mSendOrderTimeTV.setText("派单时间：" + DateUtils.format(serviceTaskBean.getTaskPostTime()));
            mTaskDetailDescTV.setText("￥" + serviceTaskBean.getTaskLucre() + "/" + serviceTaskBean.getWeight() + "公斤/" + Str.formatDoubleData(serviceTaskBean.getDistance() / 1000) + "km");
            mTakeAddressNameTV.setText(serviceTaskBean.getBuildName());
            mTakeAddressDetailsTV.setText(serviceTaskBean.getAddress());
            mSendAddressNameTV.setText(serviceTaskBean.getGiveBuildName());
            mSendAddressDetailsTV.setText(serviceTaskBean.getGiveAddress());
            mSendContractTV.setText("寄件人：" + serviceTaskBean.getContactName());
            mReceiverContractTV.setText("收件人：" + serviceTaskBean.getGiveContactName());
            mTaskCodeTV.setText("任务编号：" + serviceTaskBean.getTaskNo());
        }
    }

    @Override
    public void onClick(View v) {
        if (serviceTaskBean == null) {
            return;
        }
        switch (v.getId()) {
            //拨打寄件人电话
            case R.id.send_phone_iv:
                DialogMgr.receiverOrSendCall(getActivityContext(), serviceTaskBean.getPhone(), 1,serviceTaskBean.getAddress(),null);
                break;
            //拨打收件人电话
            case R.id.receiver_phone_iv:
                DialogMgr.receiverOrSendCall(getActivityContext(), serviceTaskBean.getGivePhone(), 2,serviceTaskBean.getGiveAddress(),serviceTaskBean.getContactName());
                break;
        }
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            //包裹列表返回数据
            case MsgID.courier_get_task_detail_data:
                cancelLoadingDialog();
                final ReqResult<TaskDetailBackBean> result = JSON.parser(msg.obj, TaskDetailBackBean.class);
                if (checkLoginStatus(result)) {
                    getActivityContext().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            TaskDetailBackBean backBean = result.getResult();
                            if (null != backBean) {
                                ServiceTaskBean bean = backBean.getTask();
                                if (bean != null) {

                                }
                                mAdapter.setItems(backBean.getDetails(), true);
                            }
                        }
                    });
                } else {
                    showToast(result.getMessage());
                }
                return true;
            default:
                return super.handleMessage(msg);
        }

    }
}
